//
//  PMDialog.cpp
//
//  Copyright (C) 2007 Visual.Wei
//  Author: Wei Shuai <cpuwolf@sina.com>
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
//

#include <windows.h>
#include <commctrl.h>
#include "progbar.h"
#include "PMDialog.h"
//------------------------------------------------
PMDialog * PMDialog::m_pclass=NULL;
PMDialog::PMDialog(HWND hWnd,LPCTSTR lpTemplate,DLGPROC DProc):PMControl()
{
	m_pclass=this;
	g_pDialogFunc=DialogProc;
	m_pTemplate=lpTemplate;
	g_hpwnd=hWnd;

}
DWORD PMDialog::ReSize()
{
	RECT rect,irect;
	
	//get parent window handler
	GetClientRect(g_hpwnd,&rect);
	TabCtrl_GetItemRect(g_hpwnd,0,&irect);
	rect.top+=(irect.bottom-irect.top+4);
	rect.bottom-=4;
	rect.left+=4;
	rect.right-=4;
	MoveWindow(g_hwnd,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,true);
	return true;
}
INT_PTR CALLBACK PMDialog::DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	PMDialog * pclass=(PMDialog *)ControlList.GetClassfromHandle(hwndDlg);
	switch (uMsg)
    {
        case WM_INITDIALOG:
			ControlList.Add(hwndDlg,(PMControl*)lParam);
			pclass=(PMDialog *)ControlList.GetClassfromHandle(hwndDlg);
			pclass->g_hwnd=hwndDlg;
			pclass->Update();
			pclass->InitShDialog(hwndDlg);
            return (INT_PTR)pclass->InitDialog(wParam,lParam);
		//case WM_ERASEBKGND:
		//	return pclass->EraseBackground((HDC)wParam);
		//case WM_PAINT:
		//	return pclass->Paint((HDC)wParam);
		case WM_TIMER:
			return pclass->Timer((int)wParam,(TIMERPROC *) lParam);
        case WM_COMMAND:
			return pclass->Command(HIWORD(wParam),LOWORD(wParam),(HWND) lParam);
            switch(LOWORD(wParam))
            {
			case IDOK:
				//delete
				EndDialog(hwndDlg, LOWORD(wParam));
				break;
            }
            break;

        case WM_DESTROY:
			ControlList.Del(hwndDlg);
			pclass->OnDestroy();
            EndDialog(hwndDlg, uMsg);
            return TRUE;
	}
		return 0;
}